package com.fruit.controller.app.login;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fruit.controller.base.BaseController;
import com.fruit.pojo.user.UserLogin;
import com.fruit.service.user.UserManager;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;
import com.fruit.util.ValidateUtil;

@Controller
@RequestMapping(value = "/app/")
public class LoginController extends BaseController {

	
	@Autowired
	private UserManager userService;

	// app快捷登录
	@RequestMapping(value = "loginToCode", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> loginToCode(UserLogin ul, HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "快捷登录");
		Map<String, Object> resMap = new HashMap<String, Object>();
		String type=request.getParameter("type");
		try {
			if(StringUtils.isEmpty(type)){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "no-type", "参数有误!"));
				return resMap;
			}
			ResultInfo ri = ValidateUtil.phone(ul.getPhone());
			if (ri != null) {
				resMap.put("result", ri);
				return resMap;
			}
				ri = ValidateUtil.smsCode(ul.getSmsCode());
				if (ri != null) {
					resMap.put("result", ri);
					return resMap;
				}
			resMap = userService.updateCodeToLogin(ul,type);

		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}
	
	// app账号登录
		@RequestMapping(value = "loginToPwd", method = { RequestMethod.GET, RequestMethod.POST })
		@ResponseBody
		public Map<String, Object> loginToPwd(UserLogin ul, HttpServletResponse response, HttpServletRequest request) {
			this.logBefore(logger, "账号登录");
			Map<String, Object> resMap = new HashMap<String, Object>();
			try {
				if(ul==null){
					resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "no-param", "参数有误!"));
					return resMap;
				}
				if (StringUtils.isEmpty(ul.getPwd())||StringUtils.isEmpty(ul.getPhone())) {
					resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "no-param", "参数有误!"));
					return resMap;
				}
				ResultInfo ri = ValidateUtil.phone(ul.getPhone());
				if (ri != null) {
					resMap.put("result", ri);
					return resMap;
				}
				
				resMap = userService.updatePwdToLogin(ul);

			} catch (Exception e) {
				e.printStackTrace();
				this.logError(logger, e.getMessage());
				resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
			}
			this.logAfter(logger);
			return resMap;
		}

	// app用户注册
	@RequestMapping(value = "register", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> register(UserLogin ul, HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "用户注册");
		Map<String, Object> resMap = new HashMap<String, Object>();
		String type=request.getParameter("type");
		try {
			ResultInfo ri = ValidateUtil.phone(ul.getPhone());
			if (ri != null) {
				resMap.put("result", ri);
				return resMap;
			}
			ri = ValidateUtil.smsCode(ul.getSmsCode());
			if (ri != null) {
				resMap.put("result", ri);
				return resMap;
			}
			if(StringUtils.isEmpty(ul.getPwd())){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "缺少参数！"));
				return resMap;
			}
			if(StringUtils.isEmpty(type)){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "参数有误！"));
				return resMap;
			}
			resMap = userService.saveUser(ul,type);
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}

	// 忘记密码
	@RequestMapping(value = "updatePwd", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> updatePwd(UserLogin ul,String type,HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "忘记密码");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			ResultInfo ri = ValidateUtil.phone(ul.getPhone());
			if (ri != null) {
				resMap.put("result", ri);
				return resMap;
			}
			ri = ValidateUtil.smsCode(ul.getSmsCode());
			if (ri != null) {
				resMap.put("result", ri);
				return resMap;
			}
			if(StringUtils.isEmpty(type)){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "参数有误！"));
				return resMap;
			}
			resMap = userService.updateUserPwd(ul,type);

		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}
	
}
